package cn.openea.perpartner.meily.web.service.impl;

import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.common.core.utils.StringUtils;
import cn.openea.perpartner.meily.web.entity.dto.InvalidSysUserTemplateDTO;
import cn.openea.perpartner.meily.web.entity.dto.UserDetailDTO;
import cn.openea.perpartner.meily.web.entity.vo.SysUserSearchVo;
import cn.openea.perpartner.meily.web.mapper.SysUserMapper;
import cn.openea.perpartner.meily.web.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author ysk
 * @version 1.0.1
 * @Description
 * @createTime 2023年04月12日 16:14:00
 */
@Service("sysUserService")
public class SysUserImpl implements ISysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<UserDetailDTO> findList(SysUserSearchVo searchVo) {
        return sysUserMapper.findList(searchVo);
    }

    @Override
    public List<UserDetailDTO> findInvalidList(SysUserSearchVo searchVo) {
        return sysUserMapper.findInvalidList(searchVo);
    }

    @Override
    public void deleteInvalidUser(List<String> userIds) {
        if (userIds == null || userIds.isEmpty()) {
            throw new ServiceException("userIds is null Or userIds is empty");
        }
        sysUserMapper.deleteInvalidUserByUserIds(userIds);
    }

    @Override
    public void addInvalidUser(String userId) {
        if (StringUtils.isBlank(userId)) {
            throw new ServiceException("userId is blank");
        }
        UserDetailDTO userDetailDTO = Optional.ofNullable(sysUserMapper.findSysUserById(userId))
                .orElseThrow(() -> new ServiceException("用户不存在"));
        UserDetailDTO invalidUser = sysUserMapper.findInvalidUserByUserId(userId);
        if (invalidUser != null) {
            throw new ServiceException("该用户已在名单中");
        }
        sysUserMapper.insertInvalidUser(userId, userDetailDTO.getNickName());
    }

    @Override
    public void saveBatchInvalidUser(List<InvalidSysUserTemplateDTO> invalidUserList) {
        if (invalidUserList == null || invalidUserList.isEmpty()) {
            throw new ServiceException("invalidUserList is null or invalidUserList is empty");
        }
        // 先查出所有数据
        List<UserDetailDTO> invalidList = sysUserMapper.findInvalidList(new SysUserSearchVo());
        // 将这个转为map
        Map<String, String> map = invalidList.parallelStream()
                .filter(item -> item.getUserId() != null && !"".equals(item.getUserId()))
                .collect(Collectors.toMap(
                        item -> Optional.ofNullable(item.getUserId()).orElse("-1"),
                        item -> Optional.ofNullable(item.getNickName()).orElse("-1"),
                        (v1, v2) -> v1 + v2));
        // 过滤掉已经存在的数据
        List<InvalidSysUserTemplateDTO> insertList = invalidUserList.parallelStream()
                .filter(item -> map.get(item.getUserId()) == null)
                .filter(item -> item.getUserId() != null && !"".equals(item.getUserId()))
                .collect(Collectors.toList());
        if (insertList.isEmpty()) {
            return;
        }
        sysUserMapper.batchInsertInvalidUser(insertList);
    }
}
